<template>
  <view class="page container">
    <u-form
      :model="basicForm"
      :rules="rules"
      labelPosition="top"
      ref="form"
      labelWidth="auto"
      :labelStyle="{
        fontSize: '14px',
        color: '#B2B2B2',
        lineHight: '20px',
      }"
    >
      <view class="basic-style">
        <u-form-item label="项目名称" prop="projectId" required>
          <h-select-project-input-popup
            :proDisabled="true"
            :projectStatus="'2'"
            ref="projectSelect"
            v-model="basicForm.projectId"
            style="width: 100%"
            placeholder="请选择项目名称"
            border="surround"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
            @change="projectChangeHandle"
          ></h-select-project-input-popup>
        </u-form-item>
        <u-form-item label="业绩归属年度" prop="year">
          <h-select-input-tag
            disabled
            style="width: 100%"
            v-model="basicForm.year"
            objectField="basicForm.year"
            placeholder="请选择业绩归属年度"
            border="surround"
            :dictName="'yjgsnd'"
          ></h-select-input-tag>
            <!-- @change="lxrChange" -->
        </u-form-item>
        <u-form-item label="签订单位" prop="signUnit">
          <h-select-company-input-popup
            disabled
            style="width: 100%"
            v-model="basicForm.signUnit"
            commercialType="1"
            placeholder="请选择签订单位"
            border="surround"
          ></h-select-company-input-popup>
        </u-form-item>
        <u-form-item label="业务名称" prop="taskId" required>
          <h-select-task-file-input-popup
            :projectId="basicForm.projectId"
            v-model="basicForm.taskId"
            class="w-100"
            :disabled="!basicForm.projectId"
            :placeholder="basicForm.projectId ? '请选择业务名称' : '请先选择项目'"
            searchPlaceholder="请输入业务名称"
            border="surround"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
            @change="taskChangeBackHandle"
            :customParams="{
              changeStatus: '!changing',
              distributionType: '!waiting',
            }"
            @select="basicForm.taskDescription = $event.data.taskDescription"
          ></h-select-task-file-input-popup>
        </u-form-item>
        <u-form-item label="费用所属团队" prop="feeTeamId" required>
          <h-select-team-input-popup
            style="width: 100%"
            v-model="basicForm.feeTeamId"
            objectField="basicForm.feeTeamId"
            placeholder="请选择费用所属团队"
            border="surround"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
            :tdlx="3"
          />
        </u-form-item>
        <u-form-item label="费用所属年度" prop="feeYear">
          <h-select-input-tag
            disabled
            style="width: 100%"
            dictName="tdnd_type"
            v-model="basicForm.feeYear"
            placeholder="请选择费用所属年度"
            border="surround"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
          ></h-select-input-tag>
        </u-form-item>
        <u-form-item label="合同额（元）" prop="contractAmount">
          <h-money-input placeholder="请输入合同额" v-model="basicForm.contractAmount" disabled></h-money-input>
        </u-form-item>
        <u-form-item label="分配合同额（元）" prop="taskContractAmount">
          <h-money-input placeholder="请输入分配合同额" v-model="basicForm.taskContractAmount" disabled></h-money-input>
        </u-form-item>
        <u-form-item label="回款额（元）" prop="realCollectionPaymentMoney">
          <h-money-input placeholder="请输入回款额" v-model="basicForm.realCollectionPaymentMoney" disabled></h-money-input>
        </u-form-item>
        <u-form-item label="审批级别" prop="approvalLevel" :borderBottom="false">
          <h-dict-select
            style="width: 100%"
            disabled
            v-model="basicForm.approvalLevel"
            placeholder="请选择审批级别"
            dictName="approval_level"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
          ></h-dict-select>
        </u-form-item>
        <u-form-item label="难易程度" prop="difficultyLevel">
          <h-select-input-tag
            disabled
            style="width: 100%"
            dictName="industry_coefficient"
            v-model="basicForm.difficultyLevel"
            placeholder="请选择难易程度"
            border="surround"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
          ></h-select-input-tag>
        </u-form-item>
        <u-form-item label="任务进度" prop="progressName">
          <u--input placeholder="请输入任务进度" v-model="basicForm.progressName" disabled></u--input>
        </u-form-item>
        <u-form-item label="报告编制绩效（元）" prop="reportAchievements" required>
          <h-money-input placeholder="请输入报告编制绩效" v-model="basicForm.reportAchievements"></h-money-input>
        </u-form-item>
        <u-form-item label="绩效提取金额（元）" prop="achievementsMoney" required>
          <h-money-input placeholder="请输入绩效提取金额" v-model="basicForm.achievementsMoney"></h-money-input>
        </u-form-item>
      </view>

      <view class="space-x-1 mt-1" style="display: flex">
        <u-button type="primary" text="关闭" :plain="true" :loading="loading" @click="onClose"></u-button>
        <u-button type="primary" text="保存" :loading="loading" @click="onSubmit"></u-button>
      </view>
    </u-form>
  </view>
</template>

<script>
// import http from '@/utils/request/index'
import {
  // add,
  // edit,
  // info,
  queryTeamById,
  queryProgressPaymentlist,
  queryProgressTaskProjectList,
} from '@/api/project/ErpAchievementsExtractionDtForm'

export default {
  name: 'ErpAchievementsExtractionDtForm',
  components: {},
  props: {
    teamId: '',
    companyId: '',
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    datailDisabled: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      default: null,
    },
    progressVoList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      smallList: [],
      loading: false,
      basicForm: {
        projectId: '',
        year: '',
        signUnit: '',
        taskId: '',
        feeTeamId: '',
        feeYear: '',
        contractAmount: '',
        taskContractAmount: '',
        realCollectionPaymentMoney: '',
        approvalLevel: '',
        difficultyLevel: '',
        progressName: '',
        reportAchievements: '',
        achievementsMoney: '',
      },
      rules: {
        projectId: [{ required: true, message: '请选择项目名称!', trigger: ['blur', 'change'] }],
        taskId: [{ required: true, message: '请选择业务名称!', trigger: ['blur', 'change'] }],
        feeTeamId: [{ required: true, message: '请选择费用所属团队!', trigger: ['blur', 'change'] }],
        reportAchievements: [{ required: true, message: '请输入报告编制绩效!', trigger: ['blur', 'change'] }],
        achievementsMoney: [{ required: true, message: '请输入绩效提取金额!', trigger: ['blur', 'change'] }],
      },
      modelNew: {},
    }
  },
  created() {},
  computed: {},
  methods: {
    async projectChangeHandle(item) {
      // 项目切换重新带数据
      if (JSON.stringify(this.modelNew) === '{}' || (this.modelNew.projectId && item.id !== this.modelNew.projectId)) {
        //清除项目数据
        this.resetDate()
        //清除任务
        this.$set(this.basicForm, 'taskId', null)
        //带出业绩归属年度
        this.$set(this.basicForm, 'year', item.year)
        //带出签订单位
        this.$set(this.basicForm, 'signUnit', item.signUnit)

        //带出合同总额和实收款总额
        await queryProgressPaymentlist({ projectId: item.id, contractType: '1' }).then((res) => {
          if (res.success) {
            //如果有项目收款合同
            if (res.result.records.length > 0) {
              //带出合同额
              this.$set(this.basicForm, 'contractAmount', res.result.records[0].contractTotalMoney)
              //带出回款额
              this.$set(this.basicForm, 'realCollectionPaymentMoney', res.result.records[0].realCollectionPaymentMoney)
            } else {
              //没有收款合同则合同额和回款额置空
              //清除合同额
              this.$set(this.basicForm, 'contractAmount', null)
              //清除回款额
              this.$set(this.basicForm, 'realCollectionPaymentMoney', null)
            }
          }
        })
        this.modelNew = JSON.parse(JSON.stringify(this.basicForm))
      }
    },
    taskChangeBackHandle(item) {
      if (item) {
        //先清空任务带出的数据
        this.resetDate('1')
        //带出费用所属团队
        this.$set(this.basicForm, 'feeTeamId', item.productTeam)
        //带出费用团队时也带出团队年度
        queryTeamById({ id: item.productTeam }).then((res) => {
          if (res.success) {
            this.$set(this.basicForm, 'feeYear', res.result.tdnd)
          }
        })
        //带出任务进度中任务进度名称
        queryProgressTaskProjectList({ projectTaskId: item.id }).then((res) => {
          if (res.success) {
            if (res.result.records.length > 0) {
              this.$set(this.basicForm, 'progressName', res.result.records[0].taskProgress)
            } else {
              this.$set(this.basicForm, 'progressName', null)
            }
          }
        })
        //带出分配合同额
        this.$set(this.basicForm, 'taskContractAmount', item.contractAmount)
        //带出复杂程度
        this.$set(this.basicForm, 'difficultyLevel', item.complexity)
        //带出审批级别
        this.$set(this.basicForm, 'approvalLevel', item.approvalLevel)
      } else {
        // 清空
        this.resetDate('1')
      }
    },
    //清空数据
    resetDate(type = '99999') {
      //清空任务带出的数据
      if (type === '1') {
        this.$set(this.basicForm, 'feeTeamId', null)
        this.$set(this.basicForm, 'feeYear', null)
        this.$set(this.basicForm, 'progressName', null)
        this.$set(this.basicForm, 'taskContractAmount', null)
        this.$set(this.basicForm, 'difficultyLevel', null)
        this.$set(this.basicForm, 'approvalLevel', null)
      } else if (type === '2') {
        //清空项目带出的数据
        this.$set(this.basicForm, 'year', null)
        this.$set(this.basicForm, 'signUnit', null)
        this.$set(this.basicForm, 'contractAmount', null)
        this.$set(this.basicForm, 'realCollectionPaymentMoney', null)
      } else {
        //清空所有带出的数据
        this.$set(this.basicForm, 'feeTeamId', null)
        this.$set(this.basicForm, 'feeYear', null)
        this.$set(this.basicForm, 'progressName', null)
        this.$set(this.basicForm, 'taskContractAmount', null)
        this.$set(this.basicForm, 'difficultyLevel', null)
        this.$set(this.basicForm, 'approvalLevel', null)
        this.$set(this.basicForm, 'year', null)
        this.$set(this.basicForm, 'signUnit', null)
        this.$set(this.basicForm, 'contractAmount', null)
        this.$set(this.basicForm, 'realCollectionPaymentMoney', null)
      }
    },

    reset() {
      this.$refs.form.resetFields()
    },
    async onSubmit() {
		const self = this
      await this.$refs.form.validate()
      if (!self.index) {
        this.$emit('submit', [
          {
            id: parseInt(Math.random() * 1000000000000),
            ...self.basicForm,
          },
          self.index,
        ])
      } else {
        this.$emit('submit', [self.basicForm, self.index])
      }
      // this.reset()
    },
    onClose() {
      this.$emit('close')
      this.reset()
    },
  }
}
</script>

<style scoped>
.page.container {
  padding: 20px;
}
.basic-style {
  margin: 0;
  max-height: 80vh;
  overflow-y: auto;
}
.u-form-item {
  padding: 0px 8px;
}
</style>
